/**
 * 作者: wgx
 * 日期: 2024-12-20 10:07:37
 * 备注: 用户荣誉奖励的进度值和领取记录
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUserHonor;
import com.motu.vertx.module.utility.model.BaseModel;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;

public class UserHonorModel extends BaseModel implements Model {
    public static final String LISTKEY = "user_id.honor_type";
    public static final int REDIS_EXPIRE_TIME = 0;// redis过期时间
    public static final int UPDATE_DB_TYPE = UPDATE_DB_TYPE_QUEUE;// 数据入库类型 队列入库
    public static final boolean ORDER_LIST = false;// 是否需要额外的有序列表

    protected long id; // 数据库id
    protected long userId; // 用户id
    protected int honorType; // 荣誉类型
    protected int num; // 进度值
    protected String getReward = ""; // 已领取奖励列表
    protected long resetTime; // 重置时间根据不同的荣誉类型进行重置

    public void reset() {
    }

    protected String getKey() {
        return "UserHonor." + userId + "." + honorType;
    }

    protected String getListKey() {
        return "UserHonor." + userId;
    }

    protected String getListItemKey() {
        return honorType + "";
    }

    public void doCreate() {
        setTableId();
        insert();
    }

    public void setTableId() {
        putId(incrTableId("user_honor"));
    }

    public JsonObject insert() {
        return super.insert(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    public void delete() {
        super.delete(getKey(), id, UPDATE_DB_TYPE, userId, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    /**
     * 根据入库类型更新
     */
    public JsonObject update() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, userId, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    /**
     * 实时入队列
     */
    public JsonObject updateQueueNow() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    public JsonObject getAllObj(){
        JsonObject obj = new JsonObject();
        obj.put("id", id); // 数据库id
        obj.put("userId", userId); // 用户id
        obj.put("honorType", honorType); // 荣誉类型
        obj.put("num", num); // 进度值
        obj.put("getReward", getReward); // 已领取奖励列表
        obj.put("resetTime", resetTime); // 重置时间根据不同的荣誉类型进行重置
        return obj;
    }

    public PBUserHonor.Builder takeInitBuilder() {
        PBUserHonor.Builder sb = PBUserHonor.newBuilder();
        if (honorType != 0) {
            sb.setHonorType(honorType);
        }
        if (num != 0) {
            sb.setNum(num);
        }
        if (this.getReward != null && this.getReward.length() > 0) {
            JsonArray list = new JsonArray(this.getReward);
            for(int i=0; i<list.size(); i++) {
                sb.addGetReward(list.getInteger(i));
            }
        }
        return sb;
    }

    public PBUserHonor.Builder takeBuilder() {
        if (updateObj == null || updateObj.isEmpty()) {
            return takeInitBuilder();
        }
        PBUserHonor.Builder sb = PBUserHonor.newBuilder();
        if (isFieldUpdate("honorType")) {
            sb.setHonorType(honorType);
        }
        if (isFieldUpdate("num")) {
            sb.setNum(num);
        }
        if (isFieldUpdate("getReward")) {
            if (this.getReward != null && this.getReward.length() > 0) {
                JsonArray list = new JsonArray(this.getReward);
                for(int i=0; i<list.size(); i++) {
                    sb.addGetReward(list.getInteger(i));
                }
                if (list.isEmpty()) {
                    sb.clearGetReward();
                }
            } else {
                sb.clearGetReward();
            }
        }
        return sb;
    }

    public long getId() {
        return id;
    }

    public void putId(long id) {
        if (this.id != id) {
            this.id = id;
            updateRedisHashField(getKey(), "id", this.id);
        }
    }

    public void addId(long value) {
        this.id += value;
        updateRedisHashField(getKey(), "id", this.id);
    }

    public long getUserId() {
        return userId;
    }

    public void putUserId(long userId) {
        if (this.userId != userId) {
            this.userId = userId;
            updateRedisHashField(getKey(), "userId", this.userId);
        }
    }

    public void addUserId(long value) {
        this.userId += value;
        updateRedisHashField(getKey(), "userId", this.userId);
    }

    public int getHonorType() {
        return honorType;
    }

    public void putHonorType(int honorType) {
        if (this.honorType != honorType) {
            this.honorType = honorType;
            updateRedisHashField(getKey(), "honorType", this.honorType);
        }
    }

    public void addHonorType(int value) {
        this.honorType += value;
        updateRedisHashField(getKey(), "honorType", this.honorType);
    }

    public int getNum() {
        return num;
    }

    public void putNum(int num) {
        if (this.num != num) {
            this.num = num;
            updateRedisHashField(getKey(), "num", this.num);
        }
    }

    public void addNum(int value) {
        this.num += value;
        updateRedisHashField(getKey(), "num", this.num);
    }

    public JsonArray getGetRewardArray() {
        if (this.getReward != null && this.getReward.length() > 0) {
            return new JsonArray(this.getReward);
        } else {
            return new JsonArray();
        }
    }

    public String getGetReward() {
        return getReward;
    }

    public void putGetReward(String getReward) {
        if (!this.getReward.equals(getReward)) {
            this.getReward = getReward;
            updateRedisHashField(getKey(), "getReward", this.getReward);
        }
    }

    public long getResetTime() {
        return resetTime;
    }

    public void putResetTime(long resetTime) {
        if (this.resetTime != resetTime) {
            this.resetTime = resetTime;
            updateRedisHashField(getKey(), "resetTime", this.resetTime);
        }
    }

    public void addResetTime(long value) {
        this.resetTime += value;
        updateRedisHashField(getKey(), "resetTime", this.resetTime);
    }

}